package com.example.cadmin.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.Archives;
import com.example.cadmin.entity.BorrowingRecords;
import com.example.cadmin.log.FileLogInsert;
import com.example.cadmin.mapper.ArchivesMapper;
import com.example.cadmin.mapper.BorrowingRecordsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.security.dto.JwtUserDto;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借阅记录 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-02-20
 */
@Service
public class BorrowingRecordsService extends ServiceImpl<BorrowingRecordsMapper, BorrowingRecords> {
    @Resource
    public BorrowingRecordsMapper borrowingRecordsMapper;
    @Resource
    public ArchivesMapper archivesMapper;

    /**
     * 借阅记录-借出档案-表格接口
     *
     * @param page
     * @param limit
     * @return
     */
    public Map<String, Object> recordsList(String fileName, int page, int limit, int dataStatus, HttpServletRequest request) {
        String number = Convert.toStr(request.getSession().getAttribute("number"));
        Map<String, Object> paramsMap = new HashMap<>();
        // 手动处理分页
        int offset = (page - 1) * limit;
        paramsMap.put("page", offset);
        paramsMap.put("limit", limit);
        if (StrUtil.isNotEmpty(fileName)) {
            paramsMap.put("fileName", fileName);
        }
        paramsMap.put("dataStatus", dataStatus);
        paramsMap.put("number", number);
        List<Map<String, Object>> mapList = borrowingRecordsMapper.recordsList(paramsMap);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("data", mapList);
        map.put("count", borrowingRecordsMapper.recordsListCount(paramsMap));
        return map;
    }

    /**
     * 借阅审批list接口
     * @param page
     * @param limit
     * @return
     */
    public Map<String, Object> approvalList(String fileName,String userName, int page, int limit, HttpServletRequest request) {
        String number = Convert.toStr(request.getSession().getAttribute("number"));
        IPage<Map<String, Object>> approvalPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        QueryWrapper<BorrowingRecords> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(fileName)) {
            wrapper.like("file_name", fileName);
        }
        if (StrUtil.isNotEmpty(userName)) {
            wrapper.like("borrowers", userName);
        }

        wrapper.eq("data_status", 1).eq("status", 1).orderByDesc("create_time").eq("number",number);
        approvalPage = borrowingRecordsMapper.selectMapsPage(approvalPage, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", approvalPage.getTotal());
        map.put("data", approvalPage.getRecords());
        return map;
    }

    /**
     * 审批状态保存
     */
    public ResponseModel approvalSave(JSONObject data) {
        JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userName = jwtUserDto.getMyUser().getUserName();
        UpdateWrapper<BorrowingRecords> borrowingRecordsUpdateWrapper = new UpdateWrapper<>();
        borrowingRecordsUpdateWrapper.eq("id", data.getInt("id")).set("approval_status", data.getInt("approval_status"))
                .set("approver",userName);
        int updateState = borrowingRecordsMapper.update(null, borrowingRecordsUpdateWrapper);
        archivesMapper.update(null,new UpdateWrapper<Archives>().eq("file_name", data.getStr("file_name"))
                .set("status", 1));
        //记录取出日志
        FileLogInsert.insert("档案管理", "取出" + data.getStr("file_name") + "档案", 4, data.getStr("number"));
        return updateState > 0 ? ResponseModel.success("审批成功") : ResponseModel.fail("审批失败");
    }
}
